<template>
	<view>
		<view class="goods-item">
			<!-- 左侧盒子 -->
			<view class="goods-item-left-box">

				<radio  :checked="item.goods_state" color="#C00000" v-if="showRadio" 
				@click="radioChangeHandler"></radio>

				<image class="goods-small-logo" :src="item.goods_small_logo" mode=""></image>
			</view>
			<!-- 右侧盒子 -->
			<view class="goods-item-right-box">
				<text>{{item.goods_name}}</text>
				
				<view class="priceAndNum">
					<view class="goods-item-right-box-price" style="color: red;">{{item.goods_price | tofixed }} 元</view>
					<uni-number-box :min="1" :value="item.goods_count" v-if="showRadio"  @change="numChangeHandler"></uni-number-box>
				</view>
				
		
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "my-goods",
		props: {
			item: {
				type: Object,
				default: {}
			},
			//是否展示radios
			showRadio: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {

			};
		},
		methods: {
			// radio 组件的点击事件处理函数
			radioChangeHandler() {
				// 通过 this.$emit() 触发外界通过 @ 绑定的 radio-change 事件，
				// 同时把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
				this.$emit('radio-change', {
					// 商品的 Id
					goods_id: this.item.goods_id,
					// 商品最新的勾选状态
					goods_state: !this.item.goods_state
				})
			},
			 // NumberBox 组件的 change 事件处理函数
			  numChangeHandler(val) {
			    // 通过 this.$emit() 触发外界通过 @ 绑定的 num-change 事件
			    this.$emit('num-change', {
			      // 商品的 Id
			      goods_id: this.item.goods_id,
			      // 商品的最新数量
			      goods_count: val+0
			    })
			  }
		},

		// 设置过滤器 使价格保留小数点后2位
		filters: {
			tofixed(num) {
				return Number(num).toFixed(2)
			}
		}
	}
</script>



<style lang="scss">
	.goods-item {
		display: flex;
		margin: 5px;
		border-bottom: 1px solid #f0f0f0;

		.goods-small-logo {
			margin-right: 10px;
			width: 100px;
			height: 100px;
		}

		.goods-item-right-box {
			font-size: 14px;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.goods-item-right-box-price {
				font-size: 18px;
			}
		}

		.goods-item-left-box {
			display: flex;
			justify-content: space-between;
			margin-left: 5px;
			align-items: center;
		}
		.priceAndNum{
			display: flex;
			justify-content: space-between;
			margin: 5px 0px;
		}

	}
</style>
